home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor.zip / CHAP9.TXT < prev    next >
Text File  |  1987-07-04  |  31KB  |  653 lines

  1.                      Chapter 9 - Standard Input/Output
  2.  
  3.  
  4.                           THE STDIO.H HEADER FILE
  5.  
  6.              Load  the file SIMPLEIO.C for our first look at a  file
  7.         with  standard I/O.   Standard I/O refers to the most  usual
  8.         places  where  data is either read from,  the  keyboard,  or
  9.         written to, the video monitor.  Since they are used so much,
  10.         they are used as the default I/O devices and do not need  to
  11.         be  named in the Input/Output instructions.   This will make
  12.         more  sense when we actually start to use them so lets  look
  13.         at the file in front of you.
  14.  
  15.              The  first thing you will notice is the first  line  of
  16.         the  file,  the #include "stdio.h" line.   This is very much
  17.         like  the  #define  we have  already  studied,  except  that
  18.         instead of a simple substitution,  an entire file is read in
  19.         at  this  point.   The  system  will  find  the  file  named
  20.         "stdio.h"  and read its entire contents in,  replacing  this
  21.         statement.   Obviously then,  the file named "stdio.h"  must
  22.         contain  valid  C source statements that can be compiled  as
  23.         part  of  a program.   This particular file is  composed  of
  24.         several standard #defines to define some of the standard I/O
  25.         operations.   The file is called a header file and you  will
  26.         find several different header files on the source disks that
  27.         came  with your Turbo C compiler.  Each of the header  files
  28.         has  a  specific  purpose  and any or all  of  them  can  be
  29.         included in any program.
  30.  
  31.              The  Turbo  C compiler uses the double quote  marks  to
  32.         indicate  that the search for the "include" file will  begin
  33.         in  the  current directory, and if it not found  there,  the
  34.         search will continue in the "include" directory as set up in
  35.         the  environment. It also uses the "less than" and  "greater
  36.         than" signs to indicate that the file search should begin in
  37.         the  directory  specified in the environment.  Most  of  the
  38.         programs  in  this tutorial have the double  quotes  in  the
  39.         "include" statements.  The next program uses the "<" and ">"
  40.         to  illustrate the usage.  Note that this will result  is  a
  41.         slightly  faster  (but  probably  unnoticeable)  compilation
  42.         because  the  system will not bother to search  the  current
  43.         directory.
  44.  
  45.                         INPUT/OUTPUT OPERATIONS IN C
  46.  
  47.              Actually  the  C programming language has no  input  or
  48.         output operations defined as part of the language, they must
  49.         be user defined.   Since everybody does not want to reinvent
  50.         his  own input and output operations,  the compiler  writers
  51.         have done a lot of this for us and supplied us with  several
  52.         input  functions and several output functions to aid in  our
  53.         program development.   The functions have become a standard,
  54.         and  you  will find the same functions available  in  nearly
  55.  
  56.  
  57.                                   Page 59
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                      Chapter 9 - Standard Input/Output
  68.  
  69.  
  70.         every  compiler.   In fact,  the industry standard of the  C
  71.         language  definition has become the book written by Kernigan
  72.         and Ritchie, and they have included these functions in their
  73.         definition.   You will often,  when reading literature about
  74.         C,  find  a  reference to K & R.   This refers to  the  book
  75.         written  by Kernigan and Ritchie.   You would be advised  to
  76.         purchase a copy for reference.
  77.  
  78.              You should print out the file named "stdio.h" and spend
  79.         some  time studying it.   There will be a lot that you  will
  80.         not understand about it, but parts of it will look familiar.
  81.         The  name  "stdio.h"  is  sort  of  cryptic  for   "standard
  82.         input/output header",  because that is exactly what it does.
  83.         It  defines  the standard input and output functions in  the
  84.         form of #defines and macros.  Don't worry too much about the
  85.         details  of this now.   You can always return to this  topic
  86.         later  for  more study if it interests  you,  but  you  will
  87.         really  have no need to completely understand the  "stdio.h"
  88.         file.  You will have a tremendous need to use it however, so
  89.         these comments on its use and purpose are necessary.
  90.  
  91.                             OTHER INCLUDE FILES
  92.  
  93.              When  you  begin writing larger programs and  splitting
  94.         them  up into separately compiled portions,  you  will  have
  95.         occasion  to  use  some  statements common to  each  of  the
  96.         portions.   It would be to your advantage to make a separate
  97.         file  containing  the  statements and use  the  #include  to
  98.         insert it into each of the files.  If you want to change any
  99.         of the common statements,  you will only need to change  one
  100.         file  and  you will be assured of having all of  the  common
  101.         statements  agree.   This  is  getting  a  little  ahead  of
  102.         ourselves  but  you  now  have  an  idea  how  the  #include
  103.         directive can be used.
  104.  
  105.                     BACK TO THE FILE NAMED "SIMPLEIO.C"
  106.  
  107.              Lets  continue our tour of the file in  question.   The
  108.         one  variable  "c" is defined and a message is  printed  out
  109.         with the familiar "printf" function.  We then find ourselves
  110.         in  a continuous loop as long as "c" is not equal to capital
  111.         X.   If  there is any question in your mind about  the  loop
  112.         control, you should review chapter 3 before continuing.  The
  113.         two  new functions within the loop are of paramount interest
  114.         in this program since they are the new functions.  These are
  115.         functions to read a character from the keyboard and  display
  116.         it on the monitor one character at a time.
  117.  
  118.              The  function "getchar()" reads a single character from
  119.         the  standard  input  device,  the  keyboard  being  assumed
  120.         because that is the standard input device, and assigns it to
  121.  
  122.  
  123.                                   Page 60
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                      Chapter 9 - Standard Input/Output
  134.  
  135.  
  136.         the variable "c".   The next function "putchar(c)", uses the
  137.         standard output device,  the video monitor,  and outputs the
  138.         character contained in the variable "c".   The character  is
  139.         output  at  the  current cursor location and the  cursor  is
  140.         advanced  one space for the next character.   The system  is
  141.         therefore taking care of a lot of the overhead for us.   The
  142.         loop  continues reading and displaying characters  until  we
  143.         type a capital X which terminates the loop.
  144.  
  145.              Compile and run this program for a few surprises.  When
  146.         you type on the keyboard, you will notice that what you type
  147.         is displayed faithfully on the screen,  and when you hit the
  148.         return key,  the entire line is repeated.   In fact, we only
  149.         told  it  to output each character once but it seems  to  be
  150.         saving  the  characters up and redisplaying them.   A  short
  151.         explanation is in order.
  152.  
  153.                DOS IS HELPING US OUT (OR GETTING IN THE WAY)
  154.  
  155.              We need to understand a little bit about how DOS  works
  156.         to  understand  what is happening here.   When data is  read
  157.         from  the keyboard,  under DOS control,  the characters  are
  158.         stored  in  a buffer until a carriage return is  entered  at
  159.         which  time the entire string of characters is given to  the
  160.         program.   When the characters are being typed, however, the
  161.         characters are displayed one at a time on the monitor.  This
  162.         is called echo,  and happens in many of the applications you
  163.         run.
  164.  
  165.              With  the above paragraph in mind,  it should be  clear
  166.         that when you are typing a line of data into "SIMPLEIO", the
  167.         characters are being echoed by DOS,  and when you return the
  168.         carriage,  the characters are given to the program.  As each
  169.         character  is given to the program,  it displays it  on  the
  170.         screen  resulting  in  a repeat of the line  typed  in.   To
  171.         better  illustrate  this,  type  a line  with  a  capital  X
  172.         somewhere  in the middle of the line.   You can type as many
  173.         characters  as you like following the "X" and they will  all
  174.         display because the characters are being read in under  DOS,
  175.         echoed  to the monitor,  and placed in the DOS input buffer.
  176.         DOS doesn't think there is anything special about a  capital
  177.         X.   When the string is given to the program,  however,  the
  178.         characters  are  accepted by the program one at a  time  and
  179.         sent  to  the monitor one at a time,  until a capital  X  is
  180.         encountered.   After the capital X is displayed, the loop is
  181.         terminated,  and the program is terminated.   The characters
  182.         on  the input line following the capital X are not displayed
  183.         because the capital X signalled program termination.
  184.  
  185.              Compile  and  run  "SIMPLEIO.C".    After  running  the
  186.         program  several  times  and  feeling  confidant  that   you
  187.  
  188.  
  189.                                   Page 61
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                      Chapter 9 - Standard Input/Output
  200.  
  201.  
  202.         understand  the above explanation,  we will go on to another
  203.         program.
  204.  
  205.              Don't  get  discouraged by the  above  seemingly  weird
  206.         behavior  of the I/O system.   It is strange,  but there are
  207.         other ways to get data into the computer.  You will actually
  208.         find the above method useful for many applications,  and you
  209.         will probably find some of the following useful also.
  210.  
  211.                          ANOTHER STRANGE I/O METHOD
  212.  
  213.              Load  the file named SINGLEIO.C and display it on  your
  214.         monitor for another method of character I/O.  Once again, we
  215.         start with the standard I/O header file using the "<" and ">
  216.         method of defining it. Then we define a variable named  "c",
  217.         and  finally  we print a welcoming message.  Like  the  last
  218.         program,  we  are in a loop that will  continue  to  execute
  219.         until  we  type  a capital X, but the  action  is  a  little
  220.         different here.
  221.  
  222.              The  "getch()"  is  a  new  function  that  is  a  "get
  223.         character" function.  It differs from "getchar()" in that it
  224.         does  not  get tied up in DOS.   It reads the  character  in
  225.         without echo, and puts it directly into the program where it
  226.         is operated on immediately.  This function therefore reads a
  227.         character,  immediately  displays  it  on  the  screen,  and
  228.         continues the operation until a capital X is typed.
  229.  
  230.              When  you compile and run this program,  you will  find
  231.         that there is no repeat of the lines when you hit a carriage
  232.         return,  and  when  you  hit  the  capital  X,  the  program
  233.         terminates immediately.  No carriage return is needed to get
  234.         it to accept the line with the X in it.   We do have another
  235.         problem  here,  however,  there  is  no  linefeed  with  the
  236.         carriage return.
  237.  
  238.                           NOW WE NEED A LINE FEED
  239.  
  240.              It  is not apparent to you in most application programs
  241.         but  when  you hit the enter key,  the  program  supplies  a
  242.         linefeed to go with the carriage return.  You need to return
  243.         to  the  left side of the monitor and you also need to  drop
  244.         down  a line.   The linefeed is not automatic.  We  need  to
  245.         improve  our program to do this also.   If you will load and
  246.         display the program named BETTERIN.C, you will find a change
  247.         to incorporate this feature.
  248.  
  249.              In BETTERIN.C, we have two additional statements at the
  250.         beginning  that  will  define the character  codes  for  the
  251.         linefeed (LF), and the carriage return (CR).  If you look at
  252.         any  ASCII table you will find that the codes 10 and 13  are
  253.  
  254.  
  255.                                   Page 62
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                      Chapter 9 - Standard Input/Output
  266.  
  267.  
  268.         exactly  as  defined  here.   In  the  main  program,  after
  269.         outputting the character,  we compare it to CR, and if it is
  270.         equal to CR,  we also output a linefeed which is the LF.  We
  271.         could have just left out the two #define statements and used
  272.         "if  (c  ==  13)  putchar(10);" but it  would  not  be  very
  273.         descriptive  of what we are doing here.  The method used  in
  274.         the program represents better programming practice.
  275.  
  276.              Compile  and  run BETTERIN.C to see if it does what  we
  277.         have said it should do.   It should display exactly what you
  278.         type in, including a linefeed with each carriage return, and
  279.         should stop immediately when you type a capital X.
  280.  
  281.                            WHICH METHOD IS BEST?
  282.  
  283.              We have examined two methods of reading characters into
  284.         a  C program,  and are faced with a choice of which  one  we
  285.         should  use.   It really depends on the application  because
  286.         each  method has advantages and disadvantages.   Lets take a
  287.         look at each.
  288.  
  289.              When using the first method,  DOS is actually doing all
  290.         of  the  work for us by storing the characters in  an  input
  291.         buffer and signalling us when a full line has been  entered.
  292.         We  could write a program that,  for example,  did a lot  of
  293.         calculations,  then  went to get some input.   While we were
  294.         doing the calculations,  DOS would be accumulating a line of
  295.         characters  for  us,  and they would be there when  we  were
  296.         ready  for  them.   However,  we could not  read  in  single
  297.         keystrokes   because  DOS  would  not  report  a  buffer  of
  298.         characters to us until it recognized a carriage return.
  299.  
  300.              The second method, used in BETTERIN.C, allows us to get
  301.         a single character,  and act on it immediately.   We do  not
  302.         have  to  wait  until  DOS decides we can  have  a  line  of
  303.         characters.  We cannot do anything else while we are waiting
  304.         for  a  character  because  we are  waiting  for  the  input
  305.         keystroke  and tying up the entire machine.   This method is
  306.         useful  for highly interactive types of program  interfaces.
  307.         It  is up to you as the programmer to decide which  is  best
  308.         for your needs.
  309.  
  310.              I  should  mention at this point that there is also  an
  311.         "ungetch" function that works with the "getch" function.  If
  312.         you "getch" a character and find that you have gone one  too
  313.         far,  you  can "ungetch" it back to the input device.   This
  314.         simplifies  some  programs because you don't know  that  you
  315.         don't  want the character until you get it.   You  can  only
  316.         "ungetch"  one character back to the input device,  but that
  317.         is  sufficient  to  accomplish the task  this  function  was
  318.         designed for.   It is difficult to demonstrate this function
  319.  
  320.  
  321.                                   Page 63
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                      Chapter 9 - Standard Input/Output
  332.  
  333.  
  334.         in  a simple program so its use will be up to you  to  study
  335.         when you need it.
  336.  
  337.              The discussion so far in this chapter, should be a good
  338.         indication  that,  while the C programming language is  very
  339.  
  340.         flexible,  it does put a lot of responsibility on you as the
  341.         programmer to keep many details in mind.
  342.  
  343.                         NOW TO READ IN SOME INTEGERS
  344.  
  345.              Load  and display the file named INTIN.C for an example
  346.         of  reading in some formatted data.   The structure of  this
  347.         program  is  very similar to the last three except  that  we
  348.         define  an "int" type variable and loop until  the  variable
  349.         somehow acquires the value of 100.
  350.  
  351.              Instead of reading in a character at a time, as we have
  352.         in the last three files,  we read in an entire integer value
  353.         with  one  call  using the  function  named  "scanf".   This
  354.         function  is very similar to the "printf" that you have been
  355.         using for quite some time by now except that it is used  for
  356.         input instead of output.   Examine the line with the "scanf"
  357.         and  you  will notice that it does not ask for the  variable
  358.         "valin"  directly,  but  gives the address of  the  variable
  359.         since it expects to have a value returned from the function.
  360.         Recall  that a function must have the address of a  variable
  361.         in  order  to  return  the value  to  the  calling  program.
  362.         Failing  to  supply  a pointer in the  "scanf"  function  is
  363.         probably  the most common problem encountered in using  this
  364.         function.
  365.  
  366.              The  function  "scanf" scans the input  line  until  it
  367.         finds  the first data field.   It ignores leading blanks and
  368.         in this case,  it reads integer characters until it finds  a
  369.         blank  or  an invalid decimal character,  at which  time  it
  370.         stops reading and returns the value.
  371.  
  372.              Remembering  our discussion above about the way the DOS
  373.         input  buffer  works,  it should be clear  that  nothing  is
  374.         actually acted on until a complete line is entered and it is
  375.         terminated by a carriage return.   At this time,  the buffer
  376.         is  input,  and  our  program will search  across  the  line
  377.         reading  all  integer values it can find until the  line  is
  378.         completely scanned.  This is because we are in a loop and we
  379.         tell it to find a value,  print it,  find another, print it,
  380.         etc.   If you enter several values on one line, it will read
  381.         each one in succession and display the values.  Entering the
  382.         value  of  100  will cause the  program  to  terminate,  and
  383.         entering  the  value 100 with other values  following,  will
  384.         cause   termination   before  the   following   values   are
  385.         considered.
  386.  
  387.  
  388.                                   Page 64
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                      Chapter 9 - Standard Input/Output
  399.  
  400.  
  401.  
  402.                       IT MAKES WRONG ANSWERS SOMETIMES
  403.  
  404.              If  you  enter a number up to and including  32767,  it
  405.         will display correctly, but if you enter a larger number, it
  406.         will appear to make an error.  For example, if you enter the
  407.         value 32768,  it will display the value of -32768,  entering
  408.         the  value  65536 will display as a  zero.   These  are  not
  409.         errors but are caused by the way an integer is defined.  The
  410.         most significant bit of the 16 bit pattern available for the
  411.         integer variable is the sign bit,  so there are only 15 bits
  412.         left  for the value.   The variable can therefore only  have
  413.         the  values  from  -32768 to 32767,  any  other  values  are
  414.         outside  the range of integer variables.   This is up to you
  415.         to take care of in your programs.   It is another example of
  416.         the increased responsibility you must assume using C  rather
  417.         than a higher level language such as Pascal, Modula-2, etc.
  418.  
  419.              Compile and run this program,  entering several numbers
  420.         on  a line to see the results,  and with varying numbers  of
  421.         blanks between the numbers.   Try entering numbers that  are
  422.         too big to see what happens,  and finally enter some invalid
  423.         characters  to  see  what the system  does  with  nondecimal
  424.         characters.
  425.  
  426.                            CHARACTER STRING INPUT
  427.  
  428.              Load  and  display  the file named  STRINGIN.C  for  an
  429.         example  of  reading  a string variable.   This  program  is
  430.         identical to the last one except that instead of an  integer
  431.         variable,  we  have defined a string variable with an  upper
  432.         limit of 24 characters (remember that a string variable must
  433.         have  a  null character at the end).   The variable  in  the
  434.         "scanf"  does  not  need  an & because  "big"  is  an  array
  435.         variable  and by definition it is already a  pointer.   This
  436.         program  should require no additional explanation.   Compile
  437.         and run it to see if it works the way you expect.
  438.  
  439.              You probably got a surprise when you ran it because  it
  440.         separated  your sentence into separate words.   When used in
  441.         the string mode of input,  "scanf" reads characters into the
  442.         string until it comes to either the end of a line or a blank
  443.         character.   Therefore,  it  reads a word,  finds the  blank
  444.         following it,  and displays the result.   Since we are in  a
  445.         loop, this program continues to read words until it exhausts
  446.         the DOS input buffer.   We have written this program to stop
  447.         whenever  it  finds a capital X in column 1,  but since  the
  448.         sentence  is split up into individual words,  it  will  stop
  449.         anytime a word begins with capital X.  Try entering a 5 word
  450.         sentence  with  a  capital X as the first character  in  the
  451.  
  452.  
  453.  
  454.                                   Page 65
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                      Chapter 9 - Standard Input/Output
  465.  
  466.  
  467.         third word.  You should get the first three words displayed,
  468.         and the last two simply ignored when the program stops.
  469.  
  470.              Try  entering more than 24 characters to see  what  the
  471.         program   does.    In  an  actual  program,   it   is   your
  472.         responsibility  to count characters and stop when the  input
  473.         buffer  is full.  You may be getting the feeling that a  lot
  474.         of  responsibility is placed on you when writing in  C.   It
  475.         is,  but  you also get a lot of flexibility in  the  bargain
  476.         too.
  477.  
  478.                        INPUT/OUTPUT PROGRAMMING IN C
  479.  
  480.              C was not designed to be used as a language for lots of
  481.         input and output,  but as a systems language where a lot  of
  482.         internal operations are required.   You would do well to use
  483.         another language for I/O intensive programming,  but C could
  484.         be used if you desire.  The keyboard input is very flexible,
  485.         allowing you to get at the data in a very low level way, but
  486.         very little help is given you.  It is therefore up to you to
  487.         take  care of all of the bookkeeping chores associated  with
  488.         your  required  I/O operations.   This may seem like a  real
  489.         pain in the neck, but in any given program, you only need to
  490.         define your input routines once and then use them as needed.
  491.  
  492.              Don't let this worry you.   As you gain experience with
  493.         C, you will easily handle your I/O requirements.
  494.  
  495.              One final point must be made about these I/O functions.
  496.         It   is  perfectly  permissible  to  intermix  "scanf"   and
  497.         "getchar"  functions during read operations.   In  the  same
  498.         manner,  it  is also fine to intermix the output  functions,
  499.         "printf" and "putchar".
  500.  
  501.                                IN MEMORY I/O
  502.  
  503.              The  next operation may seem a little strange at first,
  504.         but  you will probably see lots of uses for it as  you  gain
  505.         experience.   Load the file named INMEM.C and display it for
  506.         another  type  of I/O,  one that never accesses the  outside
  507.         world, but stays in the computer.
  508.  
  509.              In INMEM.C, we define a few variables, then assign some
  510.         values to the ones named "numbers" for illustrative purposes
  511.         and then use a "sprintf" function.   The function acts  just
  512.         like  a  normal  "printf" function except  that  instead  of
  513.         printing the line of output to a device,  it prints the line
  514.         of  formatted  output to a character string in  memory.   In
  515.         this  case  the string goes to the string  variable  "line",
  516.         because  that  is the string name we inserted as  the  first
  517.         argument  in the "sprintf" function.   The spaces after  the
  518.  
  519.  
  520.                                   Page 66
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                      Chapter 9 - Standard Input/Output
  531.  
  532.  
  533.         2nd  %d were put there to illustrate that the next  function
  534.         will  search  properly  across  the  line.    We  print  the
  535.         resulting  string and find that the output is  identical  to
  536.         what  it would have been by using a "printf" instead of  the
  537.         "sprintf"  in the first place.   You will see that when  you
  538.         compile and run the program shortly.
  539.  
  540.              Since  the generated string is still in memory,  we can
  541.         now  read  it  with the  function  "sscanf".   We  tell  the
  542.         function  in its first argument that "line" is the string to
  543.         use for its input,  and the remaining parts of the line  are
  544.         exactly  what  we  would  use if we were going  to  use  the
  545.         "scanf"  function and read data from outside  the  computer.
  546.         Note  that it is essential that we use pointers to the  data
  547.         because  we  want to return data from a function.   Just  to
  548.         illustrate  that  there are many ways to declare  a  pointer
  549.         several methods are used,  but all are pointers.   The first
  550.         two simply declare the address of the elements of the array,
  551.         while the last three use the fact that "result", without the
  552.         accompanying  subscript,  is  a pointer.   Just to  keep  it
  553.         interesting,  the  values  are read back in  reverse  order.
  554.         Finally the values are displayed on the monitor.
  555.  
  556.                            IS THAT REALLY USEFUL?
  557.  
  558.              It  seems sort of silly to read input data from  within
  559.         the  computer  but  it does have  a  real  purpose.   It  is
  560.         possible to read data from an input device using any of  the
  561.         standard  functions  and  then do  a  format  conversion  in
  562.         memory.   You  could read in a line of data, look at  a  few
  563.         significant  characters,  then  use  these  formatted  input
  564.         routines   to   reduce  the  line  of   data   to   internal
  565.         representation.  That would sure beat writing your own  data
  566.         formatting routines.
  567.  
  568.                            STANDARD ERROR OUTPUT
  569.  
  570.              Sometimes  it is desirable to redirect the output  from
  571.         the  standard  output device to a file.   However,  you  may
  572.         still  want the error messages to go to the standard  output
  573.         device,  in our case the monitor.  This next function allows
  574.         you to do that. Load and display SPECIAL.C for an example of
  575.         this new function.
  576.  
  577.              The  program  consists  of a  loop  with  two  messages
  578.         output,  one  to the standard output device and the other to
  579.         the  standard  error device.   The message to  the  standard
  580.         error  device  is  output with the  function  "fprintf"  and
  581.         includes  the  device name "stderr" as the  first  argument.
  582.         Other  than those two small changes,  it is the same as  our
  583.         standard  "printf"  function.   (You will see  more  of  the
  584.  
  585.  
  586.                                   Page 67
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                      Chapter 9 - Standard Input/Output
  597.  
  598.  
  599.         "fprintf"  function in the next chapter,  but its  operation
  600.         fit  in better as a part of this chapter.)  Ignore the  line
  601.         with the "exit" for the moment, we will return to it.
  602.  
  603.              Compile  and  run this program,  and you will  find  12
  604.         lines of output on the monitor.   To see the difference, run
  605.         the  program  again with redirected output to a  file  named
  606.         "STUFF" by entering the following line at the Dos prompt;
  607.  
  608.         A> special >stuff
  609.  
  610.              More  information about I/O redirection can be found in
  611.         your  DOS manual.   This time you will only get the 6  lines
  612.         output to the standard error device, and if you look in your
  613.         directory,  you will find the file named "STUFF"  containing
  614.         the other 6 lines, those to the standard output device.  You
  615.         can use I/O redirection with any of the programs we have run
  616.         so far,  and as you may guess, you can also read from a file
  617.         using I/O redirection but we will study a better way to read
  618.         from a file in the next chapter.
  619.  
  620.                      WHAT ABOUT THE exit(4) STATEMENT?
  621.  
  622.              Now  to  keep our promise about the exit(4)  statement.
  623.         Redisplay  the file named SPECIAL.C on  your  monitor.   The
  624.         last  statement  simply  exits the program and  returns  the
  625.         value  of 4 to DOS.   Any number from 0 to 9 can be used  in
  626.         the parentheses for DOS communication.  If you are operating
  627.         in  a  BATCH  file,  this  number can  be  tested  with  the
  628.         "ERRORLEVEL" command.
  629.  
  630.             A check of the documentation for my COMPAQ, resulted  in
  631.         a  minimal and confusing documentation of  the  "ERRORLEVEL"
  632.         command, so a brief description of it is given in this file.
  633.  
  634.  
  635.         PROGRAMMING EXERCISE
  636.  
  637.         1.   Write  a program to  read in a character using a  loop,
  638.              and  display the character in its normal  "char"  form.
  639.              Also  display  it  as a decimal  number.  Check  for  a
  640.              dollar  sign  to use as the  stop  character.  Use  the
  641.              "getch" form of input so it will print immediately. Hit
  642.              some of the special keys,  such as function keys,  when
  643.              you  run the program for some surprises.  You will  get
  644.              two  inputs  from the special keys,  the first being  a
  645.              zero  which  is  the indication to the  system  that  a
  646.              special key was hit.
  647.  
  648.  
  649.  
  650.  
  651.  
  652.                                   Page 68
  653.